#include "sn_atc.h"

static rt_err_t atc_set_once(struct atc_set_ops *atc_pos, rt_device_t dev, const struct atc_set_data *data)
{
	rt_tick_t stop = rt_tick_get() + data->timeout * RT_TICK_PER_SECOND;
	rt_size_t rdlen;
	char rdbuf[CHAT_READ_BUF_MAX];

	atc_pos->flush_rx(dev);

	if (data->transmit)
	{
		rt_kprintf(CHAT_DATA_FMT " transmit --> modem\n", CHAT_DATA_STR(data));
		atc_pos->write(dev, 0, data->transmit, rt_strlen(data->transmit), stop);
		if (data->cr == RT_TRUE)
		{
			atc_pos->write(dev, 0, "\r\n", 2, stop);
		}
	}
	do
	{
		rdlen = atc_pos->read(dev, 0, rdbuf, CHAT_READ_BUF_MAX, stop);
		rdbuf[rdlen]=0;
		rt_kprintf("rdlen:%d,rdbuf:%s\n", rdlen, rdbuf);
		if (rdlen <= 0)
		{
			rt_thread_mdelay(1000);
			continue;
		}
		else
		{
			if (rt_strstr((const char *)rdbuf, data->expect)) //接收到回复信息
			{
				return RT_EOK;
			}
			else
			{
				continue;
			}
		}
	} while (stop - rt_tick_get() < RT_TICK_MAX / 2);
	rt_kprintf(CHAT_DATA_FMT " timeout\n", CHAT_DATA_STR(data));
	return -RT_ETIMEOUT;
}
rt_err_t atc_set(struct atc_set_ops *atc_pos, rt_device_t dev, const struct atc_set_data *data, rt_size_t len)
{
	rt_err_t err = RT_EOK;
	rt_size_t i;
	rt_uint8_t retry_time;

	for (i = 0; i < len; i++)
	{
		rt_kprintf(CHAT_DATA_FMT " running\n", CHAT_DATA_STR(&data[i]));
		for (retry_time = 0; retry_time < data[i].retries; retry_time++)
		{
			err = atc_set_once(atc_pos, dev, &data[i]);
			if (err == RT_EOK)
				break;
		}
		if (err != RT_EOK)
		{
			rt_kprintf(CHAT_DATA_FMT " fail\n", CHAT_DATA_STR(&data[i]));
			break;
		}
		rt_kprintf(CHAT_DATA_FMT " success", CHAT_DATA_STR(&data[i]));
	}
	return err;
}



/*****************test**************/

//#include "serial.h"
//static const struct atc_set_data c3_cmd[] =
//{
//	{"AT", "OK", 3, 10, RT_TRUE},
//	{"AT", "OK", 3, 10, RT_TRUE}
//};

//void c3_flush(rt_device_t dev)
//{
//	char rdbuf[CHAT_READ_BUF_MAX] = {0};

//	while (rt_device_read(dev, 0, rdbuf, CHAT_READ_BUF_MAX))
//	{
//		rt_kprintf("flush rdbuf:%s\n", rdbuf);
//		rt_thread_mdelay(20);
//	}
//}
//rt_size_t c3_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size, int timeout)
//{
//	return rt_device_read(dev, 0, buffer, size);
//}
//rt_size_t c3_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size, int timeout)
//{
//	return rt_device_write(dev, 0, buffer, size);
//}

//static struct atc_set_ops c3_ops =
//{
//	c3_flush,
//	c3_read,
//	c3_write,
//};
//int atc_test(int argc, char **argv)
//{
//	/* 查找系统中的串口设备 */
//	rt_device_t serial = rt_device_find(argv[1]);
//	if (!serial)
//	{
//		rt_kprintf("find %s failed!\n", argv[1]);
//		return RT_ERROR;
//	}
//	rt_kprintf("find %s success!\n", argv[1]);

//	/* 以中断接收及轮询发送模式打开串口设备 */
//	int result = rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);

//	rt_kprintf("open %s :%d!\n", argv[1], result);

//	result = atc_set(&c3_ops, serial, c3_cmd, sizeof(c3_cmd) / sizeof(c3_cmd[0]));
//	rt_kprintf("atc_set %s :%d!\n", argv[1], result);
//	return RT_EOK;
//}
//MSH_CMD_EXPORT(atc_test, atc_test)
